/*
 * Copyright 2012 Marsrutai.lt
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android.adapter;

import java.util.List;

import lt.marsrutai.mobile.android.R;
import lt.marsrutai.mobile.android.activity.intent.FastestRoutesResultMapIntentHolder;
import lt.marsrutai.mobile.android.model.Route;
import lt.marsrutai.mobile.android.model.Segment;
import lt.marsrutai.mobile.android.model.enums.TransportType;
import lt.marsrutai.mobile.android.util.HourMinuteFormat;
import lt.marsrutai.mobile.android.widget.ListViewAdapter;
import lt.marsrutai.mobile.android.widget.ScheduleNumber;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

public class RouteAdapter extends ListViewAdapter<Route> {

    private String scopeFolder;

    public RouteAdapter(Context context, String scopeFolder) {
        super(context, R.layout.fastest_route_item);

        this.scopeFolder = scopeFolder;
    }

    @Override
    public View doGetView(int position, View view, ViewGroup parent) {

        final Route route = getItem(position);

        if (route != null) {
            TextView duration = (TextView) view.findViewById(R.id.duration);
            ViewGroup scheduleNumbers = (ViewGroup) view.findViewById(R.id.schedule_numbers);

            scheduleNumbers.removeAllViews();

            String durationText = "";
            List<Segment> segments = route.getSegments();
            for (int i = 0; i < segments.size(); i++) {
                Segment segment = segments.get(i);

                ScheduleNumber scheduleNumber = new ScheduleNumber(activity);
                if (segment.getTransportType() != TransportType.WALK) {
                    scheduleNumber.setNumber(segment.getScheduleName());
                }

                scheduleNumber.setTransportType(segment.getTransportType());
                scheduleNumber.setScope(scopeFolder);
                scheduleNumber.setTransportId(segment.getTransportId());
                scheduleNumbers.addView(scheduleNumber.getView());

                if (i == 0) {
                    durationText += HourMinuteFormat.format(segment.getStartTime()) + " - ";
                }

                if (i == segments.size() - 1) {
                    durationText += HourMinuteFormat.format(segment.getEndTime());
                }
            }

            if (duration != null) {
                duration.setText(durationText);
            }

            final String mapTitle = durationText;
            ImageButton mapButton = findViewById(view, R.id.map);
            mapButton.setFocusable(false);
            mapButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    FastestRoutesResultMapIntentHolder intentHolder = new FastestRoutesResultMapIntentHolder(activity);
                    intentHolder.setRoute(route);
                    intentHolder.setScopeFolder(scopeFolder);
                    intentHolder.setTitle(mapTitle);
                    activity.startActivity(intentHolder.getIntent());
                }
            });
        }
        return view;
    }

}
